package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author clear
 * @version 1.0
 * @description: TODO
 * @date 2023/11/27 19:16
 */
public class Demon05 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //定义了任务
        Callable<Integer> callable = new Callable<Integer>() {
            //call() 是Callable中的核心方法，返回值是integer
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for(int i = 0;i<=1000;i++){
                    sum+=i;
                }
                return sum;
            }
        };

        //把任务放到线程中执行
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();


        //这里的get() 就能获取到Callable中的num，如果线程没有执行完，就会阻塞了
        System.out.println(futureTask.get());

    }
}



